<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller;

use app\common\controller\Admin;
use app\basic\controller\api\Detection as DetectionApi;
use app\admin\controller\ajax\Library as LibarayAjax;
use app\basic\controller\api\Enterprise;
/**
 * 监测设备管理
 * @package app\rescue
 */
class Detection  extends Admin

{
    
    protected $api;
    protected $property;
    
	public function _initialize()
	{
		parent::_initialize();
		$this->api = new DetectionApi();
		$this->property = new LibarayAjax();
	}
    /**
     * 首页
     * @return Json/
     */
   	public function index() {
   	  if(IS_AJAX){
   	  	$detectionList = $this->api->detectionList($this->scope);
   	  	return $detectionList;
   	  }
   	  $enterpriseList = model('Enterprise')->getSelectList($this->scope);
	  $this->assign('enterpriseList',$enterpriseList);
      return $this->fetch();
 	}
 	

 	/**
 	 * 监测设备查询
 	 * @return Json/View
 	 */
 	public function search() {
 		if(IS_AJAX){
 			$detectionList = $this->api->search(input('keyword'),input('enterpriseId'));
 			return $detectionList;
 		}
 	}
 	
 	
 	/**
 	 * 添加监测设备
 	 * @return Json/View
 	 */
 	public function add(){
 		if(IS_POST){
 			$data = input('post.');
 			if(isset($data['isValid'])){
 				$data['isValid'] = 1;
 			}else{
 				$data['isValid'] = 0;
 			}
 			return $this->api->add($data);
 		}
 		$enterpriseList = model('Enterprise')->getSelectList($this->scope);
 		$this->assign('enterpriseList',$enterpriseList);
 		return $this->fetch();
 	}
 	
 	/**
 	 * 编辑监测设备
 	 * @param string $hazardId
 	 * @return Json/View
 	 */
 	public function edit($detectionId = null){
 		if(IS_POST){
 			$data = input('post.');
 			if(isset($data['isValid'])){
 				$data['isValid'] = 1;
 			}else{
 				$data['isValid'] = 0;
 			}
 			return $this->api->edit($data,$detectionId);
 		}
 		$enterpriseList = model('Enterprise')->getSelectList($this->scope);
 		$this->assign('enterpriseList',$enterpriseList);
 		$detection = model('Detection')->find($detectionId);
 		$this->assign('info',$detection);
 		return $this->fetch();
 		
 	}
 	
 	/**
 	 * 删除监测设备
 	 * @return Json
 	 */
 	public function del($detectionId = null) {
 		if(IS_AJAX){
 			return $this->api->del($detectionId);
 		}
 	}

	/**
	 * 查看监控设备
	 * @return Json
	 */
	public function  camera($detectionId = null){

		return $this->fetch();

	}

 	
 	/**
 	 * 设置监控位置
 	 * @param string $enterprise 企业ID
 	 * @param string $target 目标ID
 	 * @param string $position 位置
 	 * @return mixed
 	 */
 	public function setDetection($enterprise = null,$target = null, $position = null){
 		//企业位置
 		$enterpriseApi = new Enterprise();
 		$center = $enterpriseApi->getEnterprisePosition($enterprise)->getData();
 		$center = $center['code']?$center['data']:null;
 		//监视对象位置
 		$target = $this->api->getTargetPosition($target)->getData();
 		$target = $target['code']?$target['data']:null;
 		$this->assign('center',$center);
 		$this->assign('target',$target);
 		$this->assign('position',$position);
 		return $this->fetch();
 	}
 	
 	/**
 	 * 传感器
 	 * @param string $title
 	 */
 	public function sensor($title = null){
 		$sensorList = $this->api->sensorList(null,$title);
   	  	return $sensorList;
 	}
}
